﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class SysManager_UserList : BaseRptPage
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        this.Load += new System.EventHandler(Page_Load);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BinUserList();
        }
    }

    private void BinUserList()
    {
        string str = "";
        //添加子账号控制条件
        bool IsAdmin = Convert.ToBoolean(Session["IsAdmin"].ToString());
        if (IsAdmin != true && Session["UserType"].ToString() == "1")
        {
            str += " and UserType=1 ";
        }

        if (txtuser.Text != "")
        {
            str += " and ( usercode like '%" + txtuser.Text + "%' or username like '%" + txtuser.Text + "%' ) ";
        }
        this.GridView1.DataSource = mydb.DM_GetDateSet("select * from sys_loginuser where 1=1"+str);
        this.GridView1.DataBind();
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                LinkButton BtnDle = (LinkButton)e.Row.FindControl("BtnDle");
                BtnDle.Attributes.Add("onclick", "return confirm('你确认要删除：\"" + e.Row.Cells[1].Text + "\"吗?')");

                LinkButton btnStop = (LinkButton)e.Row.FindControl("btnStop");
                btnStop.Attributes.Add("onclick", "return confirm('你确认要停用：\"" + e.Row.Cells[1].Text + "\"吗?')");

                LinkButton btnOpen = (LinkButton)e.Row.FindControl("btnOpen");
                btnOpen.Attributes.Add("onclick", "return confirm('你确认要启用：\"" + e.Row.Cells[1].Text + "\"吗?')");
            }
            if (e.Row.Cells[5].Text == "True")
            {
                e.Row.Cells[5].Text = "已启用";
                e.Row.FindControl("btnOpen").Visible = false;
                e.Row.FindControl("btnStop").Visible = true;
            }
            else
            {
                e.Row.Cells[5].Text = "已停用";
                e.Row.FindControl("btnOpen").Visible = true;
                e.Row.FindControl("btnStop").Visible = false;
            }
        }
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        BinUserList();
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        Response.Redirect("UserDtl.aspx?mid=" + mid); 
    }
    protected void btnFind_Click(object sender, EventArgs e)
    {
        BinUserList();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        BinUserList();
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string userid = e.CommandArgument.ToString();
        if (e.CommandName == "BtnOpen")
        {
            if (mydb.DM_ExecuteNonQuery("update sys_loginuser set block=1 where userid='" + userid + "'") != -1)
            {
                Response.Write("<script type='text/javascript'>alert('启用成功！')</script>");
                Response.Redirect("UserList.aspx?mid="+mid);
            }
        }
        else if (e.CommandName == "btnStop")
        {
            if(mydb.DM_ExecuteNonQuery("update sys_loginuser set block=0 where userid='"+userid+"'") != -1)
            {
                Response.Write("<script type='text/javascript'>alert('停用成功！')</script>");
                Response.Redirect("UserList.aspx?mid="+mid);
            }
        }
        else if (e.CommandName == "BtnDle")
        {
            if (mydb.DM_ExecuteNonQuery("delete sys_loginuser where userid='" + userid + "'") != -1)
            {
                Response.Write("<script type='text/javascript'>alert('删除成功！')</script>");
                Response.Redirect("UserList.aspx?mid=" + mid);
            }
        }
        else if (e.CommandName == "btnDtl")
        {
            Response.Redirect("UserDtl.aspx?mid="+mid+"&UserID='" + userid + "'");
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        GridView1.AllowPaging = false;
        BinUserList();
        GridViewToExcel(this.GridView1, "application/ms-excel", this.Title + ".xls");
        GridView1.AllowPaging = true;
    }
}